package com.heima.wemadia.service.impl;

import com.github.pagehelper.PageHelper;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dto.WmSensitiveDto;
import com.heima.model.wemedia.dto.WmSensitiveListDto;
import com.heima.model.wemedia.entity.WmSensitive;
import com.heima.wemadia.dao.WmSensitiveDao;
import com.heima.wemadia.service.WmSensitiveService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 自媒体敏感词表(WmSensitive)表服务实现类
 *
 * @author makejava
 * @since 2022-04-22 18:14:12
 */
@Service("wmSensitiveService")
public class WmSensitiveServiceImpl implements WmSensitiveService {
    @Resource
    private WmSensitiveDao wmSensitiveDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public WmSensitive queryById(Long id) {
        return this.wmSensitiveDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param wmSensitive 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<WmSensitive> queryByPage(WmSensitive wmSensitive, PageRequest pageRequest) {
        long total = this.wmSensitiveDao.count(wmSensitive);
        return new PageImpl<>(this.wmSensitiveDao.queryAllByLimit(wmSensitive, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param wmSensitive 实例对象
     * @return 实例对象
     */
    @Override
    public WmSensitive insert(WmSensitive wmSensitive) {
        this.wmSensitiveDao.insert(wmSensitive);
        return wmSensitive;
    }

    /**
     * 修改数据
     *
     * @param wmSensitive 实例对象
     * @return 实例对象
     */
    @Override
    public WmSensitive update(WmSensitive wmSensitive) {
        this.wmSensitiveDao.update(wmSensitive);
        return this.queryById(wmSensitive.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.wmSensitiveDao.deleteById(id) > 0;
    }

    @Override
    public ResponseResult save(WmSensitiveDto dto) {

        if(StringUtils.isBlank(dto.getSensitives())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmSensitive wmSensitive=wmSensitiveDao.queryBySensitives(dto.getSensitives());
        if (wmSensitive != null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"敏感词已存在");
        }

        wmSensitive.setSensitives(dto.getSensitives());
        if(dto.getId() != null || dto.getId() !=0){
           wmSensitive.setId(dto.getId());
        }

        if(dto.getCreatedTime() != null){
            wmSensitive.setCreatedTime(dto.getCreatedTime());
        }else {
            wmSensitive.setCreatedTime( new Date());
        }
        wmSensitive.setUpdatedTime(new Date());

        wmSensitiveDao.insert(wmSensitive);

        return ResponseResult.okResult("添加成功");
    }

    @Override
    public ResponseResult list(WmSensitiveListDto dto) {

        dto.checkParam();
        PageHelper.startPage(dto.getPage(),dto.getSize());
        com.github.pagehelper.Page<WmSensitive> page= wmSensitiveDao.list(dto.getName());
        ResponseResult result = new PageResponseResult(page.getPageNum(), page.getPageSize(), (int) page.getTotal());

        result.setData(page.getResult());
        return result;
    }

    @Override
    public ResponseResult alter(WmSensitiveDto dto) {
        if(StringUtils.isBlank(dto.getSensitives())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        if(dto.getId() ==0 || dto.getId() == null){
            WmSensitive wmSensitive = new WmSensitive();
            wmSensitive.setSensitives(dto.getSensitives());
            wmSensitive.setCreatedTime( new Date());
            wmSensitive.setUpdatedTime(new Date());
            wmSensitiveDao.insert(wmSensitive);
        }else {
            WmSensitive wmSensitive = new WmSensitive();
            BeanUtils.copyProperties(dto,wmSensitive);
            wmSensitive.setCreatedTime(null);
            wmSensitive.setUpdatedTime(new Date());
            wmSensitiveDao.update(wmSensitive);
        }

        return ResponseResult.okResult("修改成功");
    }

    @Override
    public ResponseResult delete(Long id) {
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        wmSensitiveDao.deleteById(id);
        return ResponseResult.okResult("删除成功");
    }
}
